Completed
Push — master ( 36261e...adbdec )
by
unknown
14s queued 12s
created

toast.js ➔ show   B

Complexity

Conditions 7

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
dl 0
loc 43
rs 7.8799
c 0
b 0
f 0
1
/*!
2
  * Bootstrap toast.js v4.6.1 (https://getbootstrap.com/)
3
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
8
  typeof define === 'function' && define.amd ? define(['jquery', './util'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.jQuery, global.Util));
0 ignored issues
show
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
10
})(this, (function ($, Util) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
  var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
16
17
  function _defineProperties(target, props) {
18
    for (var i = 0; i < props.length; i++) {
19
      var descriptor = props[i];
20
      descriptor.enumerable = descriptor.enumerable || false;
21
      descriptor.configurable = true;
22
      if ("value" in descriptor) descriptor.writable = true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
23
      Object.defineProperty(target, descriptor.key, descriptor);
24
    }
25
  }
26
27
  function _createClass(Constructor, protoProps, staticProps) {
28
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
    if (staticProps) _defineProperties(Constructor, staticProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
30
    return Constructor;
31
  }
32
33
  function _extends() {
34
    _extends = Object.assign || function (target) {
35
      for (var i = 1; i < arguments.length; i++) {
36
        var source = arguments[i];
37
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. _extends is already defined in line 36 as a function. While this will work, it can be very confusing.
Loading history...
38
        for (var key in source) {
39
          if (Object.prototype.hasOwnProperty.call(source, key)) {
40
            target[key] = source[key];
41
          }
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
42
        }
43
      }
44
45
      return target;
46
    };
47
48
    return _extends.apply(this, arguments);
49
  }
50
51
  /**
52
   * Constants
53
   */
54
55
  var NAME = 'toast';
56
  var VERSION = '4.6.1';
57
  var DATA_KEY = 'bs.toast';
58
  var EVENT_KEY = "." + DATA_KEY;
59
  var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME];
60
  var CLASS_NAME_FADE = 'fade';
61
  var CLASS_NAME_HIDE = 'hide';
62
  var CLASS_NAME_SHOW = 'show';
63
  var CLASS_NAME_SHOWING = 'showing';
64
  var EVENT_CLICK_DISMISS = "click.dismiss" + EVENT_KEY;
65
  var EVENT_HIDE = "hide" + EVENT_KEY;
66
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
67
  var EVENT_SHOW = "show" + EVENT_KEY;
68
  var EVENT_SHOWN = "shown" + EVENT_KEY;
69
  var SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]';
70
  var Default = {
71
    animation: true,
72
    autohide: true,
73
    delay: 500
74
  };
75
  var DefaultType = {
76
    animation: 'boolean',
77
    autohide: 'boolean',
78
    delay: 'number'
79
  };
80
  /**
81
   * Class definition
82
   */
83
84
  var Toast = /*#__PURE__*/function () {
85
    function Toast(element, config) {
86
      this._element = element;
87
      this._config = this._getConfig(config);
88
      this._timeout = null;
89
90
      this._setListeners();
91
    } // Getters
92
93
94
    var _proto = Toast.prototype;
95
96
    // Public
97
    _proto.show = function show() {
98
      var _this = this;
99
100
      var showEvent = $__default["default"].Event(EVENT_SHOW);
101
      $__default["default"](this._element).trigger(showEvent);
102
103
      if (showEvent.isDefaultPrevented()) {
104
        return;
105
      }
106
107
      this._clearTimeout();
108
109
      if (this._config.animation) {
110
        this._element.classList.add(CLASS_NAME_FADE);
111
      }
112
113
      var complete = function complete() {
114
        _this._element.classList.remove(CLASS_NAME_SHOWING);
115
116
        _this._element.classList.add(CLASS_NAME_SHOW);
117
118
        $__default["default"](_this._element).trigger(EVENT_SHOWN);
119
120
        if (_this._config.autohide) {
121
          _this._timeout = setTimeout(function () {
122
            _this.hide();
123
          }, _this._config.delay);
124
        }
125
      };
126
127
      this._element.classList.remove(CLASS_NAME_HIDE);
128
129
      Util__default["default"].reflow(this._element);
130
131
      this._element.classList.add(CLASS_NAME_SHOWING);
132
133
      if (this._config.animation) {
134
        var transitionDuration = Util__default["default"].getTransitionDurationFromElement(this._element);
135
        $__default["default"](this._element).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
136
      } else {
137
        complete();
138
      }
139
    };
140
141
    _proto.hide = function hide() {
142
      if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
143
        return;
144
      }
145
146
      var hideEvent = $__default["default"].Event(EVENT_HIDE);
147
      $__default["default"](this._element).trigger(hideEvent);
148
149
      if (hideEvent.isDefaultPrevented()) {
150
        return;
151
      }
152
153
      this._close();
154
    };
155
156
    _proto.dispose = function dispose() {
157
      this._clearTimeout();
158
159
      if (this._element.classList.contains(CLASS_NAME_SHOW)) {
160
        this._element.classList.remove(CLASS_NAME_SHOW);
161
      }
162
163
      $__default["default"](this._element).off(EVENT_CLICK_DISMISS);
164
      $__default["default"].removeData(this._element, DATA_KEY);
165
      this._element = null;
166
      this._config = null;
167
    } // Private
168
    ;
169
170
    _proto._getConfig = function _getConfig(config) {
171
      config = _extends({}, Default, $__default["default"](this._element).data(), typeof config === 'object' && config ? config : {});
172
      Util__default["default"].typeCheckConfig(NAME, config, this.constructor.DefaultType);
173
      return config;
174
    };
175
176
    _proto._setListeners = function _setListeners() {
177
      var _this2 = this;
178
179
      $__default["default"](this._element).on(EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, function () {
180
        return _this2.hide();
181
      });
182
    };
183
184
    _proto._close = function _close() {
185
      var _this3 = this;
186
187
      var complete = function complete() {
188
        _this3._element.classList.add(CLASS_NAME_HIDE);
189
190
        $__default["default"](_this3._element).trigger(EVENT_HIDDEN);
191
      };
192
193
      this._element.classList.remove(CLASS_NAME_SHOW);
194
195
      if (this._config.animation) {
196
        var transitionDuration = Util__default["default"].getTransitionDurationFromElement(this._element);
197
        $__default["default"](this._element).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
198
      } else {
199
        complete();
200
      }
201
    };
202
203
    _proto._clearTimeout = function _clearTimeout() {
204
      clearTimeout(this._timeout);
205
      this._timeout = null;
206
    } // Static
207
    ;
208
209
    Toast._jQueryInterface = function _jQueryInterface(config) {
210
      return this.each(function () {
211
        var $element = $__default["default"](this);
212
        var data = $element.data(DATA_KEY);
213
214
        var _config = typeof config === 'object' && config;
215
216
        if (!data) {
217
          data = new Toast(this, _config);
218
          $element.data(DATA_KEY, data);
219
        }
220
221
        if (typeof config === 'string') {
222
          if (typeof data[config] === 'undefined') {
223
            throw new TypeError("No method named \"" + config + "\"");
224
          }
225
226
          data[config](this);
227
        }
228
      });
229
    };
230
231
    _createClass(Toast, null, [{
232
      key: "VERSION",
233
      get: function get() {
234
        return VERSION;
235
      }
236
    }, {
237
      key: "DefaultType",
238
      get: function get() {
239
        return DefaultType;
240
      }
241
    }, {
242
      key: "Default",
243
      get: function get() {
244
        return Default;
245
      }
246
    }]);
247
248
    return Toast;
249
  }();
250
  /**
251
   * jQuery
252
   */
253
254
255
  $__default["default"].fn[NAME] = Toast._jQueryInterface;
256
  $__default["default"].fn[NAME].Constructor = Toast;
257
258
  $__default["default"].fn[NAME].noConflict = function () {
259
    $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT;
260
    return Toast._jQueryInterface;
261
  };
262
263
  return Toast;
264
265
}));
266
//# sourceMappingURL=toast.js.map
267